package com.apexedu.sys.service.impl;
/**
 * 管理员模板控�?
 * @author zhuzhonglei
 * @time 2009-11-20
 */

import com.apexedu.biz.Const;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.sys.action.ReportFactorybaseAction;
import com.apexedu.sys.entity.TSysReportfactory;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.sys.util.CommonQuery;
import com.apexedu.util.FileUtil;
import com.apexedu.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service("reportFactorybaseService")
public class ReportFactorybaseServiceImpl extends AbstractBaseServiceSupport {

    private static final Logger logger = Logger.getLogger(ReportFactorybaseServiceImpl.class);

    public List<?> list(Object obj, Pager pager) throws Exception {
        long c=DBUtil.count("select count(*) as c from t_sys_Reportfactory t ");
        pager.setTotalRows(c);
        System.out.println("ReportFactorybaseServiceImpl"+c);
        List<TSysReportfactory> list = DBUtil.queryPageBeanList(pager,"select wid,(select code from t_db where wid=t.code) as code,chartname,applicablerole,shuom,state from t_sys_Reportfactory t", TSysReportfactory.class);
        return list;
    }

    @Override
    public void saveOrUpdate(Object myaction) throws Exception {
        ReportFactorybaseAction action = (ReportFactorybaseAction) myaction;
        TSysReportfactory reportfactory = action.getTsysReportfactory();
        File photo=action.getPhoto();
        //do update
        if (photo!= null) {
            // 有文件上传
            String str_maxphotosize = CommonQuery.getSysProperty(Const.FILE_SIZE_PHOTO);// 系统参数管理里设置的图片最大大小
            long maxPhotoSize;
            if (StringUtils.isBlank(str_maxphotosize)) {
                maxPhotoSize = Const.FILE_SIZE_1M;// 用系统默认，已经按字节计算
            } else {
                maxPhotoSize = Integer.parseInt(str_maxphotosize) * 1024;// kb转为字节
            }
            FileUtil.checkFileIsImage(action.getPhotoContentType(), action.getPhotoFileName());
            FileUtil.checkFileSize(photo.length(), maxPhotoSize);
            String relativeHttpPath = FileUtil.saveFile(photo, "cms", action.getPhotoFileName());
            reportfactory.setImages(relativeHttpPath);
        }

        if (reportfactory != null) {
            if (StringUtil.isNotBlank(reportfactory.getApplicablerole())){
                reportfactory.setApplicablerole(reportfactory.getApplicablerole().replace(" ", ""));
            }
            reportfactory.setApplicablerole(reportfactory.getUsrtype());
            if (StringUtil.isNotBlank(reportfactory.getWid())) {
                baseDao.updateNotNull(reportfactory);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_SYS_REPORTFACTORY, Const.LOG_ACTION_UPDATE,
                        IdLoginAction.getIpAddr(action.getRequest()), "修改图表工厂");
            } else {
                reportfactory.setWid(SeqFactory.getNewSequenceAlone());
                baseDao.save(reportfactory);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_SYS_REPORTFACTORY, Const.LOG_ACTION_SAVE,
                        IdLoginAction.getIpAddr(action.getRequest()),"新增图表工厂");
            }
        }
    }

    @Override
    public void load(Object myaction) throws Exception {
        ReportFactorybaseAction action = (ReportFactorybaseAction) myaction;
        String wid = action.getWid();
        TSysReportfactory reportfactory = DBUtil.queryBean("select * from t_sys_Reportfactory tsr where tsr.wid = ?", TSysReportfactory.class, wid);
        if (StringUtil.isNotBlank(reportfactory.getApplicablerole())){
            reportfactory.setApplicablerole(reportfactory.getApplicablerole().replace(" ",""));
        }
        action.setTsysReportfactory(reportfactory);
    }

    @Override
    public boolean remove(Object myaction) throws Exception {
        return false;
    }

    public String addreport(String rid,String location,Object obj) {
        ReportFactorybaseAction action = (ReportFactorybaseAction) obj;
        String wid=SeqFactory.getNewSequenceAlone();
        String sql_insert=" insert into T_USER_REPORT (WID, RWID, LOCATION, USERID)" +
                          " values ('"+wid+"', '"+rid+"', '"+location+"', '"+action.getLoginuser().getUserid()+"')";
        String sql_del=" delete T_USER_REPORT where LOCATION=? and USERID=? ";
        int i =DBUtil.executeSQL(sql_del,location,action.getLoginuser().getUserid());
        i =DBUtil.executeSQL(sql_insert);
        if (i>0){
            return "ok";
        }else{
            return "error";
        }
    }

    public String delreport(String location,Object obj) {
        ReportFactorybaseAction action = (ReportFactorybaseAction) obj;
        String sql_del="delete T_USER_REPORT t  where t.location=? and t.userid=?";
        System.out.print("sql_del="+sql_del);
        int i =DBUtil.executeSQL(sql_del,location,action.getLoginuser().getUserid());
        if (i>0){
            return "ok";
        }else{
            return "error";
        }
    }

    public boolean removePic(Object myaction){

        ReportFactorybaseAction action = (ReportFactorybaseAction)myaction;
        String wid = action.getWid();
        String pic = getBaseDao().findFieldValue("select images from t_sys_Reportfactory where wid=?", wid);
      //FileUtil.deleteFileByRelativePath(pic);
        int i = getBaseDao().executeHql("update TXxfbGg set images=null where wid=? ", wid);
        if(i>0){
            return true;
        }
        return false;
    }
}
